21 //_for(i, a, b): a <= i < b
22 #define _for(i, a, b) for (int i=a, _n = b; i<_n; ++i)
23 //_from(i, a, b): a > i >= b
24 #define _from(i, a, b) for (int i=a-1, _n = b; i>=_n; --i)
26 #define D(x) cout << #x " is " << x << endl
29 int used
, chamber
, sum
;
31 state(int used
, int i
, int s
) : used(used
), chamber(i
), sum(s
){}
33 bool operator <(const state
&that
) const {
34 return sum
> that
.sum
|| (sum
== that
.sum
&& chamber
< that
.chamber
);
38 const int oo
= INT_MAX
/ 4;
40 int d
[1<<11][6], choice
[1<<11][6];
42 #define enqueue(new_used, new_i, new_w, c) \
43 if (new_w < d[new_used][new_i]){ \
44 d[new_used][new_i] = new_w; \
45 choice[new_used][new_i] = c; \
46 q.push(state(new_used, new_i, new_w)); \
47 if (0) printf(" pushed used=%x, i=%d, w=%d\n", new_used, new_i, new_w); \
51 for (unsigned int mask
= 1 << 10; mask
; mask
>>= 1){
52 cout
<< (i
& mask
? "1" : "0");
59 int n
, m
, i
, j
, k
, Set
= 1;
60 while (cin
>> n
>> m
){
63 cin
>> p
[i
], mass
+= p
[i
];
65 for (i
=0; i
<(1<<m
); ++i
)
69 priority_queue
<state
> q
;
70 q
.push(state(0, 0, 0));
72 int used
= q
.top().used
;
73 int i
= q
.top().chamber
;
78 printf("popped used=%x, i=%d, w=%d\n", used, i, w);
85 if (i
== n
&& used
== (1 << m
) - 1){
87 printf("Set #%d\n", Set
++);
97 if (choice
[mask
][j
] & (1 << k
)) printf(" %d", p
[k
]);
101 mask
&= ~choice
[mask
][j
];
104 printf("IMBALANCE = %.5lf\n\n", 1.0 * w
/ n
);
108 if (i
> n
|| w
> d
[used
][i
]) continue;
110 enqueue(used
, i
+1, w
+ mass
, 0);
112 if ( ! (used
& (1 << j
)) ){
113 int w_extra
= abs(n
*p
[j
] - mass
);
114 enqueue(used
| (1 << j
), i
+1, w
+ w_extra
, (1 << j
));
117 if (k
!= j
&& ( ! (used
& (1 << k
)) )){
118 w_extra
= abs(n
*(p
[j
]+p
[k
]) - mass
);
119 enqueue(used
| (1 << j
) | (1 << k
), i
+1, w
+ w_extra
, (1 << j
) | (1 << k
));